<?php
namespace app\index\controller;

use think\Cache;
use think\Db;
use think\Controller;
use think\Session;

use alidayu_sms\Ali_sms;
use mredis\Mredis;
use vcode\Vcode;


class News extends Controller
{

    function __construct() {
        parent::__construct();
        $this->get_front_menu();//获取前台栏目和友情链接
        $this->get_webinfo();//获取网站优化信息
    }


    //文章详情
    public function detail($column,$name){
        //查询栏目信息
        $c_info = Db::name('column')->where('seo_name_en',$column)->find();;
        if(!$c_info){
            $this->error('栏目不存在！', '/');
        }
        $this->assign('c_info',$c_info);
        if($c_info['type']==1){//文章
            $data = Db::name('article')->where('seo_name_en',deletehtml($name))->find();
        }else{
            $data = Db::name('product')->where('seo_name_en',deletehtml($name))->find();
        }

        $web_info['web_title'] = $data['title'];
        $web_info['web_keywords'] = $data['keywords'];
        $web_info['web_descript'] = $data['describe'];
        $this->assign('data',$data);

        $this->assign('web_info',$web_info);
        $read_info = Session::get('aid_'.$name);
        if(!$read_info){
            Session::set('aid_' .$name, 1);//写入session
            Db::name('article')->where('seo_name_en', $name)->setInc('read_num');//访问数量+1
        }


        $inside_where['inside'] = 1;
        $inside_lists = Db::name('navigation')->where($inside_where)->order('sort desc')->select();//获取内页显示的栏目
        $this->assign('inside_lists',$inside_lists);

        $where['status']='1';
        $where['column_name']= $c_info['seo_name_en'];
        $lists =  Db::name('article') ->field('id,title,seo_name_en,add_time,image,describe,column_name')->where($where)->order('id desc')->limit(5)->select();//查询新闻中心文章列表
        $this->assign('lists', $lists);
        return $this->fetch('index/detail');
    }
    //获取前台栏目和友情链接
    public function get_front_menu(){
        $nav_where['status'] = 1;
        $nav_lists = Db::name('navigation')->where($nav_where)->order('sort desc')->select();
        $this->assign('nav_lists', $nav_lists);
        $friendly_where['status'] = 1;
        $friendly_link = Db::name('friendly_link')->where($friendly_where)->order('id desc')->select();
        $this->assign('friendly_link', $friendly_link);
    }
    //获取网站信息
    public function get_webinfo(){
        $web_seo = Db::name('system')->where('id',1)->find();
        $spider = robots();
        if($spider){
            $data['spider'] = $spider['spider'];
            $data['time'] = strtotime($spider['date']);
            $data['ip'] = $spider['ip'];
            $data['url'] = $spider['url'];
            $status = Db::name('spider')->insert($data);
        }
        $this->assign('web_seo', $web_seo);
    }

    //获取栏目
    public function column($column){
        //查询栏目信息
        $data = Db::name('column')->where('seo_name_en',$column)->find();
        if(!$data){
            $this->error('栏目不存在！', '/');
        }
        $web_info['web_title'] = $data['name'];
        $web_info['web_keywords'] = $data['keywords'];
        $web_info['web_descript'] = $data['describe'];
        $this->assign('web_info',$web_info);
        $data['title'] = $data['name'];
        $this->assign('data',$data);

        $inside_where['inside'] = 1;
        $inside_lists = Db::name('navigation')->where($inside_where)->order('sort desc')->select();//获取内页显示的栏目
        $this->assign('inside_lists',$inside_lists);

        $news_where['type'] = 1;
        $news_where['status'] = 1;
        $news_lists = Db::name('article')->where($news_where)->field('id,title,describe,add_time,column_name,seo_name_en')->order('id desc')->limit(6)->select();//查询点赞的所有文章
        $this->assign('news_lists', $news_lists);

        if($data['type']==1){//文章
            $where['type']='2';
            $where['column_name']= $data['seo_name_en'];
            $lists = Db::name('article')->where($where)->order('id desc')->paginate(10);//查询新闻中心文章列表
            $this->assign('lists', $lists);
            return $this->fetch('index/list_article');
        }else{
            $where['column_name']= $data['seo_name_en'];
            $lists = Db::name('product')->where($where)->order('id desc')->paginate(10);//查询新闻中心文章列表
            $this->assign('lists', $lists);
            return $this->fetch('index/list_article');
        }
    }
}